package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.ServiceOfferConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.util.DateUtils;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.CheckStartTimeAndEndTimeAttrHandler;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 *
 */
@Service
@AttrHandler(CheckStartTimeAndEndTimeAttrHandler.class)
public class CheckStartTimeAndEndTimeAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Autowired
    private OperateInstAttrService operateInstAttrService;

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        if (KiteStringUtils.isEmpty(attr.getValue())) {
            return;
        }
        //变更业务的场景初始化过程中不计算
        if (!ServiceOfferConsts.SERVICE_OFFER_ORDER.equals(data.getServiceOfferId())
            && !KeyConsts.IFTRUE_T.equals(querySceneInstService.getExtParameterValue(data.getSceneInstId(), LocalKeyConsts.BUSI_CHANGE_SCENE))) {
            return;
        }
        KiteAttr prodPeriodAttr = inst.getAttr("210004048");
        String serviceOfferId = SpringUtil.getBean(IQuerySceneInstService.class).getSceneInst(data.getSceneInstId()).getServiceOfferId();
        if (ServiceOfferConsts.SERVICE_OFFER_ORDER.equals(serviceOfferId) && Objects.isNull(prodPeriodAttr)) {
            prodPeriodAttr = inst.getAttr("200004048");
        }
        if (isPeriodProdIds(inst.getSpecId()) && Objects.nonNull(prodPeriodAttr) && KiteStringUtils.isNotEmpty(prodPeriodAttr.getValue())) {
            try {
                String periodVal = prodPeriodAttr.getValue();
                Date startDate = DateUtils.parseStrToDate(attr.getValue());
                Calendar cal = Calendar.getInstance();
                cal.setTime(startDate);
                if ("100000161".equals(periodVal)) {
                    cal.add(Calendar.MONTH, 12);//加12个月
                } else if ("100000162".equals(periodVal)) {
                    cal.add(Calendar.MONTH, 24);//加24个月
                } else {
                    cal.add(Calendar.MONTH, 36);//加36个月
                }
                cal.add(Calendar.DATE, -1);//减一天
                Date endDate = cal.getTime();
                operateInstAttrService
                    .setAttrValue(inst, "210003981", DateUtils.getDateString(endDate), data.getSceneInstId(), null);
            }
            catch (Exception e) {
                ErrorConsts.BUSI_COMMON_ERROR.throwOut("服务截止时间联动处理异常：" + e.getMessage());
            }
        }
    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }

    /**
     * 判断是否具备延期属性组产品
     * @return
     */
    private boolean isPeriodProdIds(String specId) {
        boolean result = false;
        if (KiteStringUtils.equalsAny(specId, "3000001731", "3000001732")) {
            return true;
        }
        List<DcPublic> prodIds = new ArrayList<>();
        Optional.ofNullable(DcPublicCache.getByPkey("20210729101", "PROD_SERVICE_PERIOD")).ifPresent(prodIds::addAll);
        if (KiteListUtils.isNotEmpty(prodIds)) {
            for (DcPublic dcPublic : prodIds) {
                String prodIdString = dcPublic.getCodea();
                if (KiteStringUtils.isNotEmpty(prodIdString) && prodIdString.contains("," + specId + ",")) {
                    result = true;
                    break;
                }
            }
        }
        return result;
    }
}
